﻿using Client;
using Shared.Method;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Update.Model;

namespace Update
{
    public partial class FormConfig : Form
    {
        private SOFTWARE _soft = null;
        private List<SOFTWARE> _listSoft = null;
        public FormConfig()
        {
            InitializeComponent();
        }

        //加载软件信息
        private void FormConfig_Load(object sender, EventArgs e)
        {
            this.comboBoxSoftUpdate.Items.Clear();
            SOFTWARE softW = new SOFTWARE();
            var selectSoft = MethodWcf.SelectSoftwareClient(softW);
            if (selectSoft.result)
            {
                _listSoft = selectSoft.value;
                for (int i = 0; i < _listSoft.Count; i++)
                {
                    this.comboBoxSoftUpdate.Items.Add(_listSoft[i].NAME);
                }
            }
            else
            {
                MessageBox.Show(selectSoft.message, "未能获取软件信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
        //选择软件目录
        private void textBoxFolder_Enter(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            folderBrowser.ShowDialog();
            this.textBoxFolder.Text = folderBrowser.SelectedPath;
        }


        //保存配置信息到文件夹
        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (_soft == null)
            {
                MessageBox.Show("没有软件名不能保存");
                return;
            }
            string folder = this.textBoxFolder.Text.Trim();
            if (string.IsNullOrEmpty(folder))
            {
                MessageBox.Show("请选择软件路径");
                return;
            }
            ConfigUpdate.SetXmlConfigSoftware(SharedMessage.DEFAULT, _soft.NAME);
            ConfigUpdate.SetXmlConfigSoftware(SharedMessage.FOLDER, folder);

            MessageBox.Show("信息保存完成");
            Close();
        }

        private void comboBoxSoftUpdate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string softName = this.comboBoxSoftUpdate.Text.Trim();
            var selected = (from s in _listSoft
                            where s.NAME == softName
                            select s).FirstOrDefault();

            if (selected != null)
            {
                _soft = selected;
            }
            else
            {
                MessageBox.Show("未能找到相应的软件信息");
            }
        }
    }
}
